package handler.auth;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import utils.SessionUtil;

public class AuthHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(!SessionUtil.hasLogin(ctx.channel())) {
            ctx.channel().close();
        } else {
            // 已经登录成功，不需要在进行校验
            ctx.pipeline().remove(this);
            super.channelRead(ctx, msg);
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) {
        if(SessionUtil.hasLogin(ctx.channel())) {
            System.out.println("登录验证通过，无需再次验证，AuthHandler被移除");
        } else {
            System.out.println("无登录验证，强制关闭连接");
        }
    }
}
